export function deepAll(arr) {
  let step = []
  for (let i = 0; i < arr.length; i++) {
    step.push(arr[i].deepCopy())
  }
  return step
}

export function swapY(node1, node2) {
  let y = node1.y
  node1.y = node2.y
  node2.y = y
}

export function swap(nodes, i, j) {
  let temp = nodes[i]
  nodes[i] = nodes[j]
  nodes[j] = temp
}

export function swapX(node1, node2) {
  let x = node1.x
  node1.x = node2.x
  node2.x = x
}

export function swapIndex(node1, node2) {
  let index = node1.index
  node1.index = node2.index
  node2.index = index
}

export function swapRx(node1, node2) {
  let rx = node1.rx
  node1.rx = node2.rx
  node2.rx = rx
}

export function swapRy(node1, node2) {
  let ry = node1.ry
  node1.ry = node2.ry
  node2.ry = ry
}

let dpr = window.devicePixelRatio

export function fillRect(ctx, x, y, width, height) {
  ctx.fillRect(x * dpr, y * dpr, width * dpr, height * dpr)
}

export function fillText(ctx, value, x, y) {
  ctx.fillText(value, x * dpr, y * dpr)
}

export function clearRect(ctx, x, y, width, height) {
  ctx.clearRect(x * dpr, y * dpr, width * dpr, height * dpr)
}

export function fillArc(ctx, x, y, r, srad, erad) {
  ctx.arc(x * dpr, y * dpr, r * dpr, srad * dpr, erad * dpr)
  ctx.fill()
}

export function strokeArc(ctx, x, y, r, srad, erad) {
  ctx.beginPath()
  ctx.strokeStyle = '#3679E7'
  ctx.lineWidth = 1 * dpr
  ctx.arc(x * dpr, y * dpr, r * dpr, srad * dpr, erad * dpr)
  ctx.stroke()
  ctx.lineWidth = 2 * dpr
  ctx.strokeStyle = 'black'
}

export function moveTo(ctx, x, y) {
  ctx.moveTo(x * dpr, y * dpr)
}

export function lineTo(ctx, x, y) {
  ctx.lineTo(x * dpr, y * dpr)
}

export function strokeRect(ctx, x, y, width, height) {
  ctx.rect(x * dpr, y * dpr, width * dpr, height * dpr)
  ctx.stroke()
}

export function drawArrow(ctx, x, y, dx, dy, color = 'black') {
  ctx.beginPath()
  ctx.strokeStyle = color
  moveTo(ctx, dx, dy)
  let flag = dx >= x ? 1 : 2
  let degree = Math.atan((dy - y) / (dx - x))
  lineTo(ctx, dx + 7 * Math.cos(flag * Math.PI + degree - Math.PI / 180 * 27),
    dy + 7 * Math.sin(flag * Math.PI + degree - Math.PI / 180 * 27))
  lineTo(ctx, dx + 7 * Math.cos(flag * Math.PI + degree + Math.PI / 180 * 27),
    dy + 7 * Math.sin(flag * Math.PI + degree + Math.PI / 180 * 27))
  lineTo(ctx, dx, dy)
  ctx.stroke()
  ctx.fillStyle = color
  ctx.fill()
}

export function arc(ctx, x, y, r, srad, erad) {
  ctx.arc(x * dpr, y * dpr, r * dpr, srad * dpr, erad * dpr)
}